package ${packageName};

import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import ${basePackage}.entity.${className};
import ${basePackage}.model.${className}Bean;
import ${basePackage}.mapper.${className}Mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import javax.annotation.Resource;
import java.util.*;

import java.io.Serializable;

/** 
 * 服务
 * @Version 1.0
 */
@Slf4j
@Service
public class ${className}Service extends BaseServiceImpl<${className}Mapper, ${className}> {
	
	@Transactional(rollbackFor = Exception.class)
	public boolean addData(${className}Bean param) throws Exception{
		//检测并作公共赋值
		checkAndFill(param,true);
		log.debug("新增检测通过");
		//保存
		${className} entity = param;
		return this.save(entity);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public boolean updateData(${className}Bean param) throws Exception{
		//检测并作公共赋值
		checkAndFill(param,false);
		log.debug("修改检测通过");
		//更新
		${className} newEntity = param;
		return this.updateById(newEntity);
	}

	/**
	 * 检测并作公共赋值
	 */
	private void checkAndFill(${className}Bean param, boolean newFlag) throws Exception{
		//检测赋值
		BladeUser user = AuthUtil.getUser();
		Date now = new Date();
		if(newFlag){
			//防止前台传-1的场景
			param.setId(null);
			param.setStatus(0);
			param.setIsDeleted(0);
			if (user != null) {
				param.setCreateUser(user.getUserId());
				param.setUpdateUser(user.getUserId());
			}
			param.setCreateTime(now);
			param.setUpdateTime(now);
		} else {
			if (param.getId() == null || param.getId() <= 0) {
				throw new Exception("主键不能为空");
			}
			if (user != null) {
				param.setUpdateUser(user.getUserId());
			}
			param.setUpdateTime(now);
		}
	}

	
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteData(${className}Bean param) throws Exception{
		if(param.getIdList() != null && param.getIdList().size() >0){
			UpdateWrapper<${className}> del = new UpdateWrapper<>();
			del.lambda().in(${className}::getId, param.getIdList());
			return this.remove(del);
		}
		if (param.getId() != null) {
            return this.removeById(param.getId());
        }
		return true;
	}

	
	public ${className}Bean findById(Long id) {
		${className}Bean cond = new ${className}Bean();
		cond.setId(id);
		return findBySpec(cond);
    }

	<#-- 循环类型及属性 -->
	<#list attrs as attr>
	<#if attr.ctrl.onlyQuery = "Y">
	
	public List<${className}Bean> findBy${attr.pascalName}(${attr.type} ${attr.camelName}){
		${className}Bean cond = new ${className}Bean();
		cond.set${attr.pascalName}(${attr.camelName});
		return findListBySpec(cond);
    }

	</#if>
	</#list>
	
	public ${className}Bean findBySpec(${className}Bean param) {
		List<${className}Bean> list = this.baseMapper.queryList(param);
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	
	public List<${className}Bean> findListBySpec(${className}Bean param) {
		return this.baseMapper.queryList(param);
	}

	
	public IPage<${className}Bean> findPageBySpec(IPage<${className}Bean> page, ${className}Bean param) {
		List<${className}Bean> dataList = baseMapper.queryPageList(page, param);
		return page.setRecords(dataList);
	}

	/**
	 * 字典赋值
	 */
	private List<${className}Bean> fillDict(List<${className}Bean> dataList) {
		if (dataList == null || dataList.size() == 0) {
			return Collections.emptyList();
		}
		for (${className}Bean vo : dataList) {
			//TODO
		}
		return dataList;
	}
}

